package top.yangbuyi.utils;

import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.yangbuyi.constants.Constants;
import top.yangbuyi.domain.SimpleUser;
import top.yangbuyi.domain.User;
import top.yangbuyi.vo.ActiverUser;

import java.util.List;


/**
 * @description: 杨不易网站:www.yangbuyi.top
 * @program: yangbuyiopenHis2020
 * @ClassName: ShiroSecurityUtils
 * @create: 2020-08-29 19:39
 * @author: yangbuyi
 * @since： JDK1.8
 * @ShiroSecurityUtils: 封装对象的操作$
 **/

@Slf4j
public class ShiroSecurityUtils {

	  /***
	   * @Description: 得到当前登陆的用户对象的ActiveUser
	   */
	  public static ActiverUser getCurrentActiveUser() {
			Subject subject = SecurityUtils.getSubject();
			ActiverUser activerUser = (ActiverUser) subject.getPrincipal();
			log.info("获取当前登录的用户:{ActiveUser}" + activerUser);
			return activerUser;
	  }

	  /***
	   * @Description: 得到当前登陆的用户对象User
	   */
	  public static User getCurrentUser() {
			return null != getCurrentActiveUser().getUser() ? getCurrentActiveUser().getUser() : null;
	  }

	  /***
	   * @Description: 得到当前登陆的用户对象SimpleUser
	   */
	  public static SimpleUser getCurrentSimpleUser() {
			User user = getCurrentActiveUser().getUser();
			return new SimpleUser(user.getUserId(), user.getUserName());
	  }

	  /***
	   * @Description: 得到当前登陆的用户名称
	   */
	  public static String getCurrentUserName() {
			return getCurrentActiveUser().getUser().getUserName();
	  }

	  /***
	   * @Description: 得到当前登陆对象的角色编码
	   */
	  public static List<String> getCurrentUserRoles() {
			return getCurrentActiveUser().getRoles();
	  }


	  /***
	   * @Description: 得到当前登陆对象的权限编码
	   */
	  public static List<String> getCurrentUserPermissions() {
			return getCurrentActiveUser().getPermissions();
	  }

	  /***
	   * @Description: 判断当前用户是否是超管
	   */
	  public static boolean isAdmin() {
			return getCurrentUser().getUserType().equals(Constants.USER_ADMIN);
	  }

}
